package com.online.onlineshop.controllers;

import com.online.onlineshop.entity.Comment;
import com.online.onlineshop.entity.Production;
import com.online.onlineshop.services.ForProductionService;
import com.online.onlineshop.utils.ResultForFrontEnd;
import com.online.onlineshop.utils.ReturnCode;
import com.online.onlineshop.utils.ReturnData;
import com.online.onlineshop.vo.CommentVo;
import com.online.onlineshop.vo.SalesVolumeParamVo;
import com.online.onlineshop.vo.SalesVolumeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("product")
public class ProductionController extends RootController {
    @Autowired
    ForProductionService productionService;

    @RequestMapping("listAll")
    public ReturnData<List<Production>> listAll(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "12") int limit) {
        return productionService.listAll(page, limit);
    }

    @RequestMapping({"selectByName/{name}", "selectByName"})
    public ReturnData<List<Production>> selectByName(@PathVariable(required = false) String name, @RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "12") int limit) {
        return productionService.selectByName(name, page, limit);
    }

    @PostMapping("selectByProduct")
    public ReturnData<List<Production>> selectByProduct(@RequestBody Production production, @RequestParam(defaultValue = "1") int page,
                                                        @RequestParam(defaultValue = "12") int limit) {
        return productionService.selectByProduct(production, page, limit);
    }

    @RequestMapping("selectByProduction")
    public ReturnData<List<Production>> selectByProduction(@ModelAttribute Production production, @RequestParam(defaultValue = "1") int page,
                                                           @RequestParam(defaultValue = "12") int limit, HttpSession session) {
        int uid = getUidFromSession(session);
        return productionService.selectByProduction(production, page, limit, uid);
    }

    @RequestMapping("addProduction")
    public ReturnData<String> addProduction(HttpSession session, Production production) {
        int uid = getUidFromSession(session);
        String userName = getUserNameFromSession(session);
        productionService.addProduction(uid, userName, production);
        return new ReturnData<String>(ReturnCode.SUCCESS);
    }

    @RequestMapping("updateByPid")
    public ReturnData<String> updateByPid(HttpSession session, Production production) {
        String userName = getUserNameFromSession(session);
        productionService.updateByPid(userName, production);
        return new ReturnData<String>(ReturnCode.SUCCESS);
    }

    /**
     * 商品热搜方法
     */
    @RequestMapping("selectHotList")
    public ResultForFrontEnd<List<Production>> selectHotList() {
        List<Production> productions = productionService.selectHotList();
        return new ResultForFrontEnd<>(ok, productions);
    }

    /**
     * 新到商品方法
     */
    @RequestMapping("selectNewList")
    public ResultForFrontEnd<List<Production>> selectNewList() {
        List<Production> productions = productionService.selectNewList();
        return new ResultForFrontEnd<>(ok, productions);
    }

    /**
     * 获取商品详细页信息的方法
     *
     * @param pid 为前端传送过来的商品id
     * @return 返回查询道德商品信息
     */
    @RequestMapping({"{pid}/selectByPid", "selectByPid/{pid}"})
    public ResultForFrontEnd<Production> selectById(@PathVariable("pid") Integer pid) {
        Production production = productionService.selectByPid(pid);
        return new ResultForFrontEnd<>(ok, production);
    }

    @RequestMapping("deleteById/{pid}")
    public ReturnData<String> deleteById(@PathVariable("pid") Integer pid) {
        productionService.deleteByPid(pid);
        return new ReturnData<String>(ReturnCode.SUCCESS);
    }

    @RequestMapping("selectSalesVolumeById")
    public ReturnData<List<SalesVolumeVo>> selectSalesVolumeById(@RequestParam(defaultValue = "0") int page, @ModelAttribute SalesVolumeParamVo vo,
                                                                 @RequestParam(defaultValue = "10") int limit, HttpSession session) {
        int uid = getUidFromSession(session);
        return productionService.selectSalesVolumeById(vo, uid, page, limit);
    }

}
